package com.study.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.IComplaintDAO;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.TousuDAO;
import com.study.model.pojo.Complaint;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Good;
import com.study.model.pojo.Logforcust;
import com.study.model.pojo.Repairlist;
import com.study.model.pojo.Service;
import com.study.model.pojo.User;

@org.springframework.stereotype.Service
@Transactional(rollbackOn = Exception.class)
public class TousuService {

	@Autowired
	TousuDAO mapper;
	@Autowired
	ILinkmanDAO linkmanDAO;
	@Autowired
	ICustomerDAO customerDAO;
	@Autowired
	IUserDAO userDAO;
	@Autowired
	IDictionaryDAO dictionaryDAO;
	@Autowired
	IComplaintDAO dao;
	@Autowired
	LogforcustDAO logforcustDAO;

	/**
	 * 根据条件查询售后服务
	 * @param cusid 
	 * 
	 * @param i
	 * @param j
	 * @param string
	 * @param string2
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, String selects, Integer cusid) {
		String clazz = null;
		String degree = null;
		String result = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length() > 0 && selects.startsWith("42")) {
			clazz = selects;
		} else if (selects.length() > 0 && selects.startsWith("43")) {
			degree = selects;
		} else if (selects.length() > 0 && selects.startsWith("41")) {
			result = selects;
		}
		return mapper.selectByWhere(clazz, degree, result, "%" + input + "%",cusid);
	}

	/**
	 * 根据id查询一条记录
	 * 
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		return mapper.selectById(parameter);
	}

	/**
	 * 添加一个投诉
	 * 
	 * @param theme
	 * @param useid
	 * @param type
	 * @param date1
	 * @param date2
	 * @param custid
	 * @param degree
	 * @param progress
	 * @param aresult
	 * @param spendtime
	 * @param feedback
	 * @param custback
	 * @param more
	 * @return
	 */

	public boolean addTousu(String theme, Integer useid, String type, Date date1, Integer custid,
			String degree, String progress, String aresult, String feedback, String linkman) {
		Complaint complaint = new Complaint();
		complaint.setComComplaint(linkman);
		complaint.setComDate(date1);
		complaint.setComFeedback(feedback);
		complaint.setComName(theme);
		complaint.setComTreati(progress);
		Customer customer = customerDAO.findByCusId(custid);
		complaint.setCustomer(customer);
		complaint.setDictionary1(dictionaryDAO.findByDicId(Integer.parseInt(type)));
		complaint.setDictionary2(dictionaryDAO.findByDicId(Integer.parseInt(degree)));
		complaint.setDictionary3(dictionaryDAO.findByDicId(Integer.parseInt(aresult)));
		User user2 = userDAO.findByUseId(useid);
		complaint.setUser(user2);
		complaint.setIsShow(1);
		dao.save(complaint);
		
		//添加流水信息
		String logMessage = "投诉&nbsp;&nbsp;"+theme +"===="+custid;

		logforcustDAO.insert(customer.getCusId(), new Date(), user2.getUseId(), 229, logMessage, "");

		return true;
	}

	/**
	 * 删除投诉
	 * 
	 * @param goo_id
	 */
	public void delete(int cid) {
		Complaint complaint = dao.findByComId(cid);
		if (complaint.getDictionary2().getDicId() == 433) {
			dao.delete(complaint);
		} else {
			complaint.setIsShow(0);
		}
	}

	/**
	 * 编辑
	 * 
	 * @param theme
	 * @param useid
	 * @param type
	 * @param date1
	 * @param date2
	 * @param custname
	 * @param degree
	 * @param progress
	 * @param aresult
	 * @param spendtime
	 * @param feedback
	 * @param linkman
	 * @param more
	 * @param id
	 * @return
	 */
	public boolean editorService(String theme, Integer useid, String type, Date date1, Integer custid,
			String degree, String progress, String aresult,  String feedback, Integer linid,
			String more, String id) {

		int result = mapper.update(custid,theme,useid,type,date1,linid,degree,progress,aresult,feedback,id);
		return result > 0 ? true:false;
	}

	/**
	 * 查询月度投诉类型统计
	 * 
	 * @return
	 */

	public List<List<JSONObject>> selectByclazz() {
		List<JSONObject> lsit = mapper.selectByMonthAndClazz();
		List<JSONObject> list1 = new ArrayList<>();
		List<JSONObject> list2 = new ArrayList<>();
		List<JSONObject> list3 = new ArrayList<>();
		List<JSONObject> list4 = new ArrayList<>();
		List<List<JSONObject>> list5 = new ArrayList<>();

		for (JSONObject jsonObject : lsit) {
			if (jsonObject.getInteger("type") == 420) {
				list1.add(jsonObject);
			} else if (jsonObject.getInteger("type") == 421) {
				list2.add(jsonObject);
			} else if (jsonObject.getInteger("type") == 422) {
				list3.add(jsonObject);
			} else {
				list4.add(jsonObject);
			}
		}

		sort(list1);
		list5.add(list1);
		
		sort(list2);
		list5.add(list2);
		
		sort(list3);
		list5.add(list3);
		
		sort(list4);
		list5.add(list4);
		for (List<JSONObject> list : list5) {
			System.out.println(list);
		}
		return list5;

	}

	private List<JSONObject> getMonth(List<JSONObject> lists, String[] time) {

		for (int i = 0; i < time.length; i++) {
			JSONObject object = new JSONObject();
			object.put("count", 0);
			object.put("releaseYearMonth", time[i]);
			try {
				lists.get(i);
			} catch (Exception e) {
				lists.add(i, object);
			}
		}

		sort(lists);
		return lists;
	}

	private void sort(List<JSONObject> list) {
		Collections.sort(list, new Comparator<JSONObject>() {
			@Override
			public int compare(JSONObject o1, JSONObject o2) {
				// TODO Auto-generated method stub
				return o1.getString("releaseYearMonth").compareTo(o2.getString("releaseYearMonth"));
			}
		});

	}

	/**
	 * 根据投诉结果查询分布
	 */
	public List<JSONObject> selectByResult() {
		return mapper.selectByResult();
	}
}
